import { NCollapse } from "naive-ui";
import type { CollapseProps, CollapseArrowSlotProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import type { BaseComponentType } from "../types";
import { getSlotsDom } from "../utils";
export type CollapseFactoryConstructorType = {
  props?: CollapseProps;
  defaultSlot?: BaseComponentType;
  arrowSlot?: (v?: CollapseArrowSlotProps) => BaseComponentType;
  headerExtraSlot?: (v?: CollapseArrowSlotProps) => BaseComponentType;
  attrs?: HTMLAttributes;
};
export class CollapseFactory {
  private props: CollapseProps = {};
  private attrs: HTMLAttributes = {};

  private defaultSlot: BaseComponentType = null;
  private arrowSlot: (v?: CollapseArrowSlotProps) => BaseComponentType = () =>
    null;
  private headerExtraSlot: (v?: CollapseArrowSlotProps) => BaseComponentType =
    () => null;

  constructor(data?: CollapseFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.arrowSlot) this.setArrow(data.arrowSlot);
    if (data?.props) this.setProps(data.props);
    if (data?.headerExtraSlot) this.setHeaderExtra(data.headerExtraSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: CollapseProps) {
    this.props = props;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }
  setArrow(component: (v?: CollapseArrowSlotProps) => BaseComponentType) {
    this.arrowSlot = component;
    return this;
  }
  setHeaderExtra(component: (v?: CollapseArrowSlotProps) => BaseComponentType) {
    this.headerExtraSlot = component;
    return this;
  }
  create() {
    return (
      <NCollapse {...this.attrs} {...this.props}>
        {{
          default: () => getSlotsDom(this.defaultSlot),
          arrow: (props: CollapseArrowSlotProps) =>
            getSlotsDom(this.arrowSlot(props)),
          "header-extra": (props: CollapseArrowSlotProps) =>
            getSlotsDom(this.headerExtraSlot(props)),
        }}
      </NCollapse>
    );
  }
}
